# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：data_mnist.py
#日期：2019-11-11
#备注：MNIST机器学习入门
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import numpy as np
import os
import gzip
from pycacho.cachobase.deal_threading import deal_list
from PIL import Image
import threading
import time


# 多线程
def threading_img(imgs):
    i = 0
    for img in imgs:
        i = i +1
        pil_img = Image.fromarray(np.uint8(img))             #Image.fromarray图像数据转换为PIL数据对象
        #pil_img.show() #显示图片
        pil_img.save('F:\PythonProject\Mnist\\testimage\\'+ threading.currentThread().name + '%05d'%(i) + '.png')

# 单线程
def img_down(i,img):
    pil_img = Image.fromarray(np.uint8(img))  # Image.fromarray图像数据转换为PIL数据对象
    # pil_img.show() #显示图片
    pil_img.save('F:\PythonProject\Mnist\\testimage\\' + '%05d' % (i) + '.png')

# 定义加载数据的函数，data_folder为保存gz数据的文件夹，该文件夹下有4个文件
# 'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
# 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'

def load_data(data_folder):

  files = [
      'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
      't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
  ]

  paths = []
  for fname in files:
    paths.append(os.path.join(data_folder,fname))

  with gzip.open(paths[0], 'rb') as lbpath:
    y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)

  with gzip.open(paths[1], 'rb') as imgpath:
    x_train = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)

  with gzip.open(paths[2], 'rb') as lbpath:
    y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)

  with gzip.open(paths[3], 'rb') as imgpath:
    x_test = np.frombuffer(
        imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)

  return (x_train, y_train), (x_test, y_test)

(train_images, train_labels), (test_images, test_labels) = load_data('F:\PythonProject\Mnist\\')

print(train_images.shape)
print(train_labels.shape)

def train_image():
    img = train_images          # 训练图像赋给img
    #img = img.reshape(28, 28)  # 把图像的形状变为原来的尺寸
    #print(img[0].shape)         # (28, 28)
    #print(len(img))             # 图片总数量
    for i in range(len(img)):
        img_down(i + 1, img[i])    # 下载图片

def train_label():
    label = train_labels  # 训练标签赋给label
    print(len(label))
    for i in range(len(label)):
        print(label[i])

def test_image():
    img = test_images            # 训练图像赋给img
    print(type(img),img)         # numpy.ndarray 三维列表
    #img = img.reshape(28, 28)   # 把图像的形状变为原来的尺寸
    #print(img[0].shape)         # (28, 28)
    #print(len(img))             # 图片总数量
    for i in range(len(img)):
        img_down(i + 1, img[i])    # 下载图片

def test_label():
    label = test_labels           # 训练标签赋给label
    print(type(label),label)      # numpy.ndarray 一维列表
    #print(len(label))
    #for i in range(len(label)):
    #    print(label[i])


def threading_down():
    deal_list(train_images,threading_img,10000)         # 60000/6


if __name__ == '__main__':
    #sTime = time.time()
    #train_image()
    #train_label()
    test_image()
    #test_label()
    #threading_down()
    #eTime = time.time()
    #s = eTime - sTime
    #print('花费的时间为：%.2f秒' % (s))
